#include <QtGui>
#include <iostream>
#include <QFile>

#include "client.h"

Client::Client(QWidget *parent) : QDialog(parent)
{
    usernameLabel = new QLabel("Username");
    usernameLineEdit = new QLineEdit();
    usernameLabel->setBuddy(usernameLineEdit);

    registerButton = new QPushButton("Register");
    registerButton->setEnabled(false);

    loginButton = new QPushButton("Login");
    loginButton->setEnabled(false);

    uploadButton = new QPushButton("Upload File");
    uploadButton->setVisible(false);

    statusLabel = new QLabel();

    connect(usernameLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableRegisterLoginButtons(const QString &)));
    connect(registerButton, SIGNAL(clicked()),this, SLOT(doRegister()));
    connect(loginButton, SIGNAL(clicked()),this, SLOT(doLogin()));
    connect(&registrationTcpSocket, SIGNAL(connected()), this, SLOT(sendRequest()));
    connect(&registrationTcpSocket, SIGNAL(readyRead()), this, SLOT(readResponse()));

    QHBoxLayout *topLeftLayout = new QHBoxLayout();
    topLeftLayout->addWidget(usernameLabel);
    topLeftLayout->addWidget(usernameLineEdit);

    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addStretch();
    leftLayout->addWidget(statusLabel);

    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->addWidget(registerButton);
    rightLayout->addWidget(loginButton);
    rightLayout->addWidget(uploadButton);
    rightLayout->addStretch();

    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);
}

void Client::enableRegisterLoginButtons(const QString &text)
{
    registerButton->setEnabled(!text.isEmpty());
    loginButton->setEnabled(!text.isEmpty());
}

void Client::doRegister()
{
    usernameString = usernameLineEdit->text();
    usernameLineEdit->clear();
    connectToServer(registrationTcpSocket, 4444);
}

void Client::doLogin()
{
    usernameString = usernameLineEdit->text();
    usernameLineEdit->clear();
    connectToServer(loginTcpSocket, 4455);
}

void Client::connectToServer(QTcpSocket &socket, int port)
{
    socket.connectToHost(QHostAddress::LocalHost, port);
    nextBlockSize = 0;
}


void Client::sendRequest()
{
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_5);

    out << quint16(0) << usernameString;
    out.device()->seek(0);
    out << quint16(block.size() - sizeof(quint16));
    registrationTcpSocket.write(block);
}

void Client::readResponse()
{
    QDataStream in(&registrationTcpSocket);
    in.setVersion(QDataStream::Qt_4_5);

    forever
    {
        if (nextBlockSize == 0) {
            if (registrationTcpSocket.bytesAvailable() < sizeof(quint16))
                break;
            in >> nextBlockSize;
        }

        if (nextBlockSize == 0xFFFF) {
            registrationTcpSocket.close();
            std::cout << "CLOSED CONNECTION" << std::endl;
            break;
        }

        if (registrationTcpSocket.bytesAvailable() < nextBlockSize)
            break;

        QString response;

        in >> response;

        statusLabel->setText(response);
        if (response == "Login Successful")
        {
             uploadButton->setVisible(true);
        }

        nextBlockSize = 0;
    }
}
